<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/18 22:52
 */

namespace module\user\front\form;

use mof\enum\EnableEnum;
use mof\enum\YesNoEnum;
use mof\front\Form;
use mof\utils\DictArray;
use mof\utils\ElementData;

class UserForm extends Form
{
    protected array $validate = [
        'param' => [
            'avatar', 'nickname', 'mobile', 'email', 'password', 'score/d', 'vip/d', 'vip_expire', 'status',
        ],
        'rule'  => [
            'username|昵称'          => 'require|min:4|max:20',
            'mobile|手机号'          => 'mobile|unique:user,mobile',
            'email|邮箱'             => 'email|unique:user,email',
            //'avatar|头像'            => 'update:image',
            'password|密码'          => 'length:6,20',
            'vip|是否VIP'            => 'require|in:0,1',
            'vip_expire|VIP到期时间' => 'requireIf:vip,1|date',
            'status|状态'            => 'require|in:0,1'
        ],
    ];

    protected function elements(\mof\Model $model = null): array
    {
        $values = $model ? $model->toArray() : [];
        $registerFields = config('user.register_fields', []);
        return [
            [
                "prop"  => "avatar",
                "label" => "头像",
                'type'  => 'upload:image',
                "value" => $values['avatar'] ?? '',
            ],
            [
                "prop"         => "nickname",
                "label"        => "昵称",
                "value"        => $values['nickname'] ?? '',
                "autocomplete" => "new-nickname",
                "rules"        => [
                    ["required" => true],
                ]
            ],
            [
                "prop"      => "password",
                "label"     => "密码",
                "type"      => "password",
                "autocomplete" => "new-password",
                "introEdit" => "不修改密码请留空",
            ],
            [
                "prop"  => "mobile",
                "label" => "手机号",
                "value" => $values['mobile'] ?? '',
                "rules" => [
                    ["required" => in_array('mobile', $registerFields)],
                ]
            ],
            [
                "prop"  => "email",
                "label" => "邮箱",
                "value" => $values['email'] ?? '',
                "rules" => [
                    ["required" => in_array('email', $registerFields)],
                ]
            ],
            [
                "prop"  => "score",
                "label" => "积分",
                "type"  => "input-number",
                "value" => $values['score'] ?? '',
            ],
            [
                "prop"    => "vip",
                "label"   => "VIP",
                "type"    => "select",
                "value"   => $values['vip'] ?? 0,
                "options" => YesNoEnum::toDict()->toElementData()->toSelectOptions(),
                "colSpan" => 12,
            ],
            [
                "prop"    => "vip_expire",
                "label"   => "VIP到期时间",
                "type"    => "date",
                "value"   => $values['vip_expire'] ?? '',
                "colSpan" => 12,
            ],
            [
                "prop"          => "status",
                "label"         => "状态",
                "type"          => "select",
                "value"         => $values['status'] ?? 1,
                "options"       => EnableEnum::toDict()->toElementData()->toSelectOptions(),
                '_defaultValue' => 1
            ]
        ];
    }
}